<script>
	import utils from './common/utils.js'
	import config from './config.js'
	export default {
		onLaunch: function(e) {
			// 检查是否从其他小程序跳转过来，并获取传递的数据
			if (options.query && options.query.fromMiniProgram) {
				// 根据需要处理跳转逻辑  
				uni.navigateTo({
					url: "/pages/order/lease",
				});
			}
			// 隐私政策
			// #ifdef APP-PLUS
			this.isFirstEnter();
			// #endif
			//应用启动参数
			this.onStartupScene(e.query);
			this.getTabBarLinks();

		},
		onShow: function() {
			// uni.request({
			// 	url:this.config.app_url + 'index.php/api/setting.setting/charge',
			// 	data:{},

			// 	success:(result)=> {
			// 			console.log(result,'result');
			// 	}
			// })
			// this.getSesame()
			this.getMiniProgram()
			this.getOrderConfig()
		},
		created() {

		},
		onHide: function() {
			//console.log('App Hide')
		},
		data() {
			return {
				// 客户端免押开关
				deposit: false,
				//快速审核
				quick_audit:false,
				//以租代购框显示开关
				car_lease_show:false,
				miniProgramData:{
					age: "18",
					app_id: "2021003192692876",
					car_lease: "0",
					chat_wd_id: "SCE01291712",
					cyphone: "17746826592",
					ddshow: "1",
					dp_status: "1",
					is_sesame_free_of_charge: "0",
					quick_audit: "0",
					quick_audit_money: "10",
					tenant_id: "7te_GQ6n",
					trends: "周日休息，周一早上9点开始审核订单，羊毛党勿扰，未成年禁止下单，逾期会影响您的征信，法务会起诉！",
					zmshow: "1",
					mianya:{}
				},
			}
		},
		methods: {
			//MiniProgram  小程序设置 数据配置接口
			getMiniProgram(){
				let self = this
				self._post('setting.setting/miniprogram',{},function(res){
					try{
						console.log(res,'settingRes')
						const miniProgramData = res.data.vars.values;
						if(miniProgramData) {
							self.miniProgramData = miniProgramData;
						}
						self.quick_audit = miniProgramData.quick_audit === '1' ? true : false
						self.is_sesame_free_of_charge = miniProgramData.is_sesame_free_of_charge == '1' ? true : false
						self.car_lease_show = miniProgramData.car_lease == '1' ? true : false
						self.deposit = self.is_sesame_free_of_charge;
						uni.setStorageSync('quick_audit', self.quick_audit);
						uni.setStorageSync('deposit', self.is_sesame_free_of_charge);
						uni.setStorageSync('car_lease_show', self.car_lease_show);
						
						config.server_config_data = miniProgramData;
						
						if(self.is_sesame_free_of_charge) {
							console.log('芝麻免押打开')
						}
						else{
							console.log('芝麻免押关闭')
						}
						
						if(self.quick_audit) {
							console.log('快速审核打开')
						}
						else{
							console.log('快速审核关闭')
						}
						
						if(self.car_lease_show) {
							console.log('以租代购打开')
						}
						else{
							console.log('以租代购关闭')
						}
						
					} catch(e){
						console.log('失败')
					}
				})
			},
			// 获取点汇或掌签开启情况
			getOrderConfig(){
				let self = this
				self._post('setting.setting/orderConfig',{},function(res){
					try{
						console.log(res,'settingRes')
						if(res.code==1){
							if(res.data.vars.values.zq_status=='1' || res.data.vars.values.dianhui=='1'){
								uni.setStorageSync('huoti',true)
							}else{
								uni.setStorageSync('huoti',false)
							}
						}
					} catch(e){
						console.log('失败')
					}
				})
			},
			getSesame() {
				let self = this
				this._post('setting.setting/fetchData', {},
					function(res) {
						// this.$globalState.shouldShowComponent = res.data.vars.values.is_sesame_free_of_charge === '1' ? true : false
						try {
							
							const newValue = res.data.vars.values.is_sesame_free_of_charge === '1' ? true : false
							self.deposit = res.data.vars.values.is_sesame_free_of_charge === '1' ? true : false
							const currentValue = uni.getStorageSync('deposit')
							// 仅在新值不同时更新存储
							if (currentValue !== newValue) {
								uni.setStorageSync('deposit', newValue)
								console.log('更新存储:', newValue)
							}
						} catch (e) {
							console.error('存储失败:', e)
						}
					})

			},
			isFirstEnter() {
				var firstEnter = uni.getStorageSync('firstEnter'); //同步获取缓存中是否有首次进入字段
				let self = this;
				uni.getSystemInfo({
					success(data) {
						// 如果是ios并且没有firstEnter缓存则弹出模态框
						if (data.platform == 'ios' && !firstEnter) {
							uni.navigateTo({
								url: '/pages/privacy/privacy'
							})
						}
					}
				})
			},
			updateManager: function() {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function(res2) {
							uni.showModal({
								title: '更新提示',
								content: '新版本已经准备好，即将重启应用',
								showCancel: false,
								success(res2) {
									if (res2.confirm) {
										// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
										updateManager.applyUpdate();
									}
								}
							});
						});
					}
				});
				updateManager.onUpdateFailed(function(res) {
					// 新的版本下载失败
					uni.showModal({
						title: '更新提示',
						content: '检查到有新版本，但下载失败，请检查网络设置',
						showCancel: false
					});
				});
			},
			/**
			 * 小程序启动场景
			 */
			onStartupScene(query) {
				if (!query) return;
				// 获取场景值
				let scene = utils.getSceneData(query);
				// 记录推荐人id
				let refereeId = query.referee_id;
				if (refereeId > 0) {
					if (!uni.getStorageSync('referee_id')) {
						uni.setStorageSync('referee_id', refereeId);
					}
				}
				// 记录分销人id
				let uid = scene.uid;
				if (uid > 0) {
					if (!uni.getStorageSync('referee_id')) {
						uni.setStorageSync('referee_id', uid);
					}
				}
				// 如果是h5，设置app_id
				// #ifdef  H5
				let appId = query.app_id;
				if (appId > 0) {
					this.config.app_id = appId;
				}
				// #endif
			},
			getTabBarLinks() {
				// 如果是android不处理图标,不起作用
				// #ifdef  APP-PLUS
				// let is_android = false;
				// if(uni.getSystemInfoSync().platform == 'android'){
				// 	is_android = true;
				// }
				// console.log('is_android='+is_android);
				// if(is_android){
				// 	return;
				// }
				// #endif
				return;
			},
			updateWgt(wgtUrl) {
				uni.showModal({
					title: '版本更新',
					content: '检查到有新版本，请点击更新',
					confirmText: "更新",
					showCancel: false,
					success: function(e) {
						if (e.confirm) {
							var dtask = plus.downloader.createDownload(
								wgtUrl, {},
								function(d, status) {
									uni.showToast({
										title: '下载完成',
										mask: false,
										duration: 1000
									});
									// 下载完成
									if (status == 200) {
										plus.runtime.install(d.filename, {
												force: true
											},
											function() {
												plus.nativeUI.alert(
													"已更新至最新版本，确定后将重启应用",
													function() {
														plus.runtime.restart();
													}, "更新提示", "确定");
											},
											function(e) {
												uni.showToast({
													title: '安装失败-01',
													mask: false,
													duration: 1500
												});
											});
									} else {
										uni.showToast({
											title: '更新失败-02',
											mask: false,
											duration: 1500
										});
									}
								});
							try {
								dtask.start(); // 开启下载的任务
								var prg = 0;
								var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象 
								dtask.addEventListener('statechanged', function(
									task,
									status
								) {
									// 给下载任务设置一个监听 并根据状态  做操作
									switch (task.state) {
										case 1:
											showLoading.setTitle("正在下载");
											break;
										case 2:
											showLoading.setTitle("已连接到服务器");
											break;
										case 3:
											prg = parseInt(
												(parseFloat(task.downloadedSize) /
													parseFloat(task.totalSize)) *
												100
											);
											showLoading.setTitle("  正在下载" + prg + "%  ");
											break;
										case 4:
											plus.nativeUI.closeWaiting();
											//下载完成
											break;
									}
								});
							} catch (err) {
								plus.nativeUI.closeWaiting();
								uni.showToast({
									title: '更新失败-03',
									mask: false,
									duration: 1500
								});
							}
						} else {
							//取消
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
	@import './common/iconfont.css';
	@import './common/myIcon.css';
	/*每个页面公共css */
	@import './common/style.scss';
</style>